<!DOCTYPE html>
<html ><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BLOB对象_欲穷千里目，更上一层楼</title>
<meta name="Keywords" content="网页，网页消息通信" />
<meta name="Description" content="在浏览器中，两个不同页面（A页面的window ！= B页面的window）网页之间的消息传递。" />
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/railscasts.min.css">

<style type="text/css">
#bd{padding-top:60px;background-color:#fff;font-size:1rem;font-family:"Microsoft YaHei"; margin: 0;}
#header
{
position:fixed;
width: 100%;height: 40px;
top: 0;line-height: 40px;
padding:10px 60px;font-size: 1.5em;
box-shadow: 0 0 1px rgba(0,0,0,0.25);
transition: background-color 0.3s ease-in-out;
background-color: #fff;
z-index: 5;
}
a{text-decoration: none;}
#header a span{color: #273849;}

#main {
    position: relative;
    z-index: 1;
    padding: 0 60px 30px;
    overflow-x: hidden;
}
#main .sidebar
{
    position: fixed;
    z-index: 10;
    top: 61px;
    left: 0;
    bottom: 0;
    overflow-x: hidden;
    overflow-y: auto;
	border-right: 1px solid #eaecef;
}


.sidebar .sidebar-inner {
width: 260px;
padding: 30px 0px 60px 20px;
}
.sidebar ul {
    list-style-type: none;
    margin: 0;
    line-height: 1.5em;
    padding-left: 1em;
}
.sidebar .menu-root {
    padding-left: 0;
}
.sidebar .menu-root a{color: #4f5959;}
.sidebar a:hover {border-bottom: 2px solid #42b983;}
.sidebar .menu-root li{margin-top: 0.5em;}

.content {
    position: relative;
    padding: 2.2em 0;
    max-width: 700px;
    margin: 0 auto;
    padding-left: 50px;
}
.content h1 {margin: 0 0 1em;}
.content h2 {
    margin: 45px 0 0.8em;
    padding-bottom: 0.7em;
    border-bottom: 1px solid #ddd;
}
.content h2,
.content h3 {
  pointer-events: none;
}
.content h2 a,
.content h3 a,
.content h2 button,
.content h3 button {
  pointer-events: auto;
  color: #273849;
}
.content h2:before,
.content h3:before {
  content: "";
  display: block;
  margin-top: -91px;
  height: 91px;
  visibility: hidden;
}
.content p{ line-height: 1.6rem}
.content a {
    color: #42b983;
    font-weight: 600;
}
pre code {
    overflow-x: auto;
    color: #525252;
    white-space: pre;
    padding: 1.2em 1.4em;
    line-height: 1.6rem;
    display: block;
	font-size:1.1rem;
}

#mobile-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color: #fff;
  z-index: 9;
  display: none;
  box-shadow: 0 0 2px rgba(0,0,0,0.25);
}
#mobile-bar .menu-button {
   font-size:2.7rem; color:#757575; padding-left:1rem;float:left;}
#mobile-bar .logo { display:block;text-align:center;color:#273849;}

@media screen and (max-width: 1300px){
	.content {
		margin-left: 290px;
	}
}

@media screen and (max-width: 900px) {
#main .sidebar {
    z-index: 10;
    background-color: #f9f9f9;
    height: 100%;
    top: 0;
    left: 0;
    box-shadow: 0 0 10px rgba(0,0,0,0.2);
    transition: all 0.4s cubic-bezier(0.4, 0, 0, 1);
    -webkit-transform: translate(-280px, 0);
    transform: translate(-280px, 0);
  }
  .sidebar .sidebar-inner {
    padding: 50px 10px 10px 20px;
    box-sizing: border-box;
  }
  .sidebar .sidebar-inner-index {
    padding: 10px 10px 10px 20px;
  }
  .sidebar .search-query {
    width: 200px;
    margin-bottom: 10px;
  }
  .sidebar .main-menu {
    display: block;
  }
  .sidebar.open {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
  }

	.content {
		margin: auto;
		 padding-left:0px
	}
	#header {
		display: none;
	}
	#mobile-bar {
		display: block;
	}
	#main .sidebar.open {
	-webkit-transform: translate(0, 0);
	transform: translate(0, 0);
	}
}

</style>
</head>
<body id="bd">

<div id="mobile-bar">
      <a class="menu-button" onClick="javascript:toggle('open')">≡</a>
      <a class="logo" href="javascript:;" ><span>BLOB对象</span></a>
    </div>


    <div id="header">
    <a href="javascript:;"><span>Javascript的BLOB对象</span></a>
    </div>

    <div id="main">
        <div class="sidebar">
            <div class="sidebar-inner">
                <div class="list">
                    <ul class="menu-root">
                    <li><h3>导航</h3></li>    
                    <li>
                        <a href="#introduce" >介绍</a>
                    </li>   
                    <li>
                        <a href="javascript:;" class="sidebar-link">使用场景</a>
                        <ul class="menu-sub">
                            <li><a href="#download" >安全下载(含mp3播放)</a></li>
                            <li><a href="#paste">黏贴剪切板截图</a></li>
                            <li><a href="#eval">替代eval函数功能</a></li>
                            <li><a href="#uploadDemo">大文件分片上传</a></li>
                        </ul>
                    </li>  
                    </ul>
                </div>
            </div>
        </div>

        <div class="content ">
                <h1 id="introduce">介绍</h1>
                <h2>
                    <a href="#" class="headerlink" >BLOB对象</a>
                </h2>
                <p>
                    一个Blob对象就是一个包含有只读原始数据的类文件对象。Blob对象中的数据并不一定得是JavaScript中的原生形式。File接口基于Blob，继承了Blob的功能,并且扩展支持了用户计算机上的本地文件。              
                </p>

                 <h1>使用场景</h1>
                
                <p>
                   <h2 id="WebSocket">1.安全下载(含mp3播放)</h2>
                   当我们需要用户登录之后才能获取资源的时候 <br>
        <a href="http://xv700.gitee.io/message-communication-for-web/postMessage/iframe/send.html"  target="_blank">DEMO在线预览</a> |     
        <a href="https://gitee.com/xv700/Message-communication-for-web/blob/master/postMessage/iframe"  target="_blank">点击进入获取源码</a>
                </p>
                
                <p>
                    <h2 id="postMessage">2.黏贴剪切板截图(paste)</h2>
                    获取剪切板上的数据来进行粘贴的操作。例如通过QQ截图后，需要在网页上进行粘贴操作。<br>
                </p>
<p>
      
        
        <a href="http://xv700.gitee.io/message-communication-for-web/postMessage/iframe/send.html"  target="_blank">DEMO在线预览</a> |     
        <a href="https://gitee.com/xv700/Message-communication-for-web/blob/master/postMessage/iframe"  target="_blank">点击进入获取源码</a>
        
        <pre>
        <code class="hljs html">//这里是发送代码 send.html
<span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">'utf-8'</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Post Message<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"width:200px; float:left;"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"color"</span>&gt;</span>Frame Color<span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"border:3px solid #0F0; width:100%; float:left"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>这里是iframe<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">iframe</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"child"</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"accept.html"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">iframe</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span>&gt;</span><span class="javascript">

        <span class="hljs-built_in">window</span>.onload=<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span></span>{
            <span class="hljs-comment">//Window.frames +索引值（命名或数字）</span>
            <span class="hljs-built_in">window</span>.frames[<span class="hljs-number">0</span>].postMessage({a:<span class="hljs-number">1</span>},<span class="hljs-string">'*'</span>);

        }

        <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'message'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span></span>{
            <span class="hljs-keyword">var</span> color=e.data;
            <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'color'</span>).style.backgroundColor=color;
        },<span class="hljs-literal">false</span>);
    </span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
        </code>
    </pre>
</p>

        <p><h2 id="SharedWorker">3.替代eval函数功能</h2>
替代eval功能
           <br>
            <a href="https://xv700.gitee.io/message-communication-for-web/SharedWorker/WorkerAndSharedWorker/Worker/"  target="_blank">DEMO在线预览</a> |     
            <a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/SharedWorker/WorkerAndSharedWorker/Worker"  target="_blank">点击进入获取源码</a><br>
   
            <pre>
                <code class="hljs javascript">
<span class="hljs-keyword">var</span> w=<span class="hljs-literal">null</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">startWorker</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span>(Worker)!==<span class="hljs-string">"undefined"</span>)
  {
  <span class="hljs-keyword">if</span>(w===<span class="hljs-literal">null</span>)
  {
  w=<span class="hljs-keyword">new</span> Worker(<span class="hljs-string">"demo_workers.js"</span>);
  }
  w.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(event)</span> </span>{
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML=event.data;
    };
  }
<span class="hljs-keyword">else</span>
  {
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML=<span class="hljs-string">"Sorry, your browser does not support Web Workers..."</span>;
  }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stopWorker</span><span class="hljs-params">()</span>
</span>{ 
w.terminate();
w=<span class="hljs-literal">null</span>;
}</code></pre>        
        </p>
        <p>
            <h2 id="Server-Sent">4.大文件分片上传</h2>
           大文件分割 (slice() 方法)<br>
slice() 方法接受三个参数，起始偏移量，结束偏移量，还有可选的 mime 类型。如果 mime 类型，没有设置，那么新的 Blob 对象的 mime 类型和父级一样。
当要上传大文件的时候，此方法非常有用，可以将大文件分割分段，然后各自上传，因为分割之后的 Blob 对象和原始的是独立存在的。
           <br>
<a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/Server-Sent"  target="_blank">点击进入获取源码</a> 
<pre><code class="hljs javascript"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Server-Sent<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>Server-Sent开始<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"result"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">h2</span>&gt;</span>Server-Sent结束<span class="hljs-tag">&lt;/<span class="hljs-title">h22</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span>(EventSource)!==<span class="hljs-string">"undefined"</span>)
    {
    <span class="hljs-keyword">var</span> source=<span class="hljs-keyword">new</span> EventSource(<span class="hljs-string">"demo_sse.asp"</span>);
    <span class="hljs-built_in">console</span>.log(source)
    source.onmessage=<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(event)</span>
    </span>{
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML+=event.data + <span class="hljs-string">"&lt;br /&gt;"</span>;
    };
    }
<span class="hljs-keyword">else</span>
    {
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML=<span class="hljs-string">"抱歉，您的浏览器不支持 server-sent 事件 ..."</span>;
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
        
</code></pre></p>

        </div>
    </div>
</body>
</html>
<script >
var sidebar=document.querySelector('.sidebar');
var menuButton = document.querySelector('.menu-button')
function toggle(aa){
	sidebar.classList.toggle(aa)
}
	
document.body.addEventListener('click', function (e) {
  if (e.target !== menuButton && !sidebar.contains(e.target)) {
	sidebar.classList.remove('open')
  }
})
</script>